package cn.kgc.vue.utils;

import com.auth0.jwt.JWT;
import com.auth0.jwt.algorithms.Algorithm;

import java.util.Calendar;
import java.util.Map;

/**
 * @Author: 课工场
 * @Version: v1.0  2023/8/9
 * @Description:  token的工具类  生成token  验证token  解析token
 *
 */
public class JWTUtil {

    //秘钥
    private static String secret = "java2320";
    // token的生命周期
    private static Integer expire = 7;

    // 创建token
    public static String createToken(Map<String,Object> map){
        Calendar instance = Calendar.getInstance();
        instance.add(Calendar.DATE,expire);
        return   JWT.create().withClaim("userinfor",map)
                .withExpiresAt(instance.getTime())
                .sign(Algorithm.HMAC256(secret));
    }

    // 解析token的方法
    public static Map<String,Object> getClaim(String token){
       return   JWT.decode(token).getClaim("userinfor").asMap();
    }

    // 验证token的信息
    public static boolean verifyToken(String token){
        JWT.require(Algorithm.HMAC256(secret)).build().verify(token);
        return  true;
    }

}
